<?php class myFunctions extends saja {
######################################

# Requires
# nmap - for smb scanning
# smbclient - accessing smb shares

var $thisfile;
var $scheme;

function __construct () {
    $this->scheme = 'smb';
    $docRoot = getenv('DOCUMENT_ROOT');
    $this->thisfile =  $docRoot . 'common/file_systems/smb-fs_saja-lib.php';
    if (!isset($_SESSION['smb_user'])) {
        $_SESSION['smb_user'] = '';
        $_SESSION['smb_pass'] = '';
    }
}

function getlocalip () {
    $ret = exec("ifconfig", $lines);
    foreach($lines as $line) {
        if (strpos($line, "inet ") == "1") {
            $war = explode(" ", $line);
            if ($war[1] != "127.0.0.1") {
                return $war[1];
                break;
            }
        }
    }
}
function getsubnet () {
    $ret = exec("ifconfig", $lines);
    foreach($lines as $line) {
        if (strpos($line, "inet ") == "1") {
            $war = explode(" ", $line);
            if ($war[1] != "127.0.0.1") {
                $subnet = substr($war[1], "0", strrpos($war[1], "."));
                return $subnet . '.';
                break;
            }
        }
    }
}

function browse_smb($newpath) {
    #require('fs_smb.php');
    #$fs = new fs_smb();
    #echo $fs->browse_smb($newpath);
    #exit;
    $purl['scheme']= '';
    $purl['host'] = '';
    $purl['path'] = '';
    $purl['login'] = '';
    $purl['password'] = '';
    if (strpos(strtolower($newpath), "$this->scheme://") === 0) {
        $_SESSION['smb_path'] = $newpath;
        error_reporting(0);
        $purl = parse_url($newpath);
        error_reporting(E_ALL & ~E_NOTICE);
        if ($purl['scheme'] != 'smb') {
            $purl['scheme'] = 'smb';
        }
        $host = $purl['host'];
        $user = $purl['user'];
        $pass = $purl['pass'];
        $purl['path'] = $this->clean($purl['path'], '/', true);
        $pntr = strpos($purl['path'], '/');
        if ($pntr > 0) {
            $share = substr($purl['path'], 0, $pntr);
            $path = substr($purl['path'], $pntr);
        } else {
            $share = $purl['path'];
            $path = '';
        }

        $user = $_SESSION['smb_user'];
        $pass = $_SESSION['smb_pass'];


        $directory = substr($newpath, strlen("$this->scheme://"));
        #$directory = rtrim($directory, '/');
        $this->build_quick_link($this->scheme, $directory);


        #### Quick Link End
        if ($host == '') {
            $this->smbclient_subnet_scan();
        } elseif ($share == '') {
            $this->smbclient_share_list($host,$user,$pass);
        } else {
            $this->smbclient_dir($host,$user,$pass,$share,$path);
        }
    }
}
function smbclient_subnet_scan () {
    $localsubnet = $this->getsubnet();
    $fullcmd = "nmap --system-dns -sS -p 139 $localsubnet" . "1-255";
    $ret = exec($fullcmd, $dirout);
    $listing = false;
    $skipline = false;
    $user = '';
    $pass = '';
    foreach ($dirout as $line) {
        if (!$skipline) {
            if (strpos($line, 'Interesting ports on ') === 0) {
                $listing = true;
                $line = substr($line, strlen('Interesting ports on '));
                $ipaddress = substr($line, 0, strlen($line) - 1);
                $skipline = true; # the next line is just a header line (PORT    STATE SERVICE     VERSION)
            } elseif ($line == '' and $listing) {
                $listing = false;
                if ($smbon) {
                    $thisdevice['ip'] = $ipaddress;
                    $thisdevice['mac'] = $macaddress;
                    $thisdevice['macvendor'] = $macvendor;
                    $thisdevice['smb'] = $smbon;
                    $devicearr[] = $thisdevice;
                }
                $ipaddress = '';
                $macaddress = '';
                $macvendor = '';
                $port = '';
                $smbon= false;
            } elseif ($listing) {
                if (strpos($line, 'MAC Address: ') === 0) {
                    $line = substr($line, strlen('MAC Address: '));
                    $pntr = strpos($line, ' ');
                    if (strpos($line, ' ') > 0) {
                        $macaddress = substr($line, 0, strpos($line, ' '));
                        $macvendor = substr($line, strpos($line, ' ') + 1);
                        $macvendor = substr($macvendor, 1, strlen($macvendor) - 2);
                    } else {
                        $macaddress = $line;
                        $macvendor = 'Unknown';
                    }
                } elseif (preg_match('#^[0-9]{1,5}\/#', $line, $matches) == 1) {
                    $port = substr($matches[0], 0,strlen($matches[0]) - 1);
                    if ($port == 139) {
                        # in this case the port will always be 139
                        $line = substr($line, strpos($line, ' ') + 1);
                        if (strpos($line, 'closed') === 0) {
                            $smbon = false;
                        } elseif (strpos($line, 'open') === 0) {
                            $smbon = true;
                        }
                    }
                }
            }
        } else {
            $skipline = false;
        }
    }
    # Now show the results
    #$this->build_quick_link($host,$user,$pass,$share,$path);
    $thisimg = '/common/images/mac-48x48.png';
    foreach ($devicearr as $adev) {
        $ipaddress = $adev['ip'];
        $inputid = $this->add_hidden_input("$this->scheme://$ipaddress");
        $applink = $this->run("browse_smb($inputid:value)->$this->target_id",$this->thisfile);
        #$applink = $this->run("browse_smb('$this->scheme://".$ipaddress."')->$this->target_id",$this->thisfile);
        $res['title'] = $ipaddress;
        $res['link'] = $applink;
        $res['img'] = $thisimg;
        if ($adev['mac'] != '') {
            $res['details'] = $adev['mac'];
            if ($adev['macvendor'] != '') {
                $res['details'] .= " - " . $adev['macvendor'];
            }
        } else {
            $res['details'] = '';
        }
        $results[] = $res;
    }
    $this->display_results($results);
}
function smbclient_share_list ($host, $user, $pass, $show_sys_shares = false) {
    $fullcmd = "smbclient -L //$host -U$user%$pass";
    $ret = exec($fullcmd, $dirout);
    $listingtype = '';
    $sharecnt = 0;
    #echo "<li style=\"color:black;\">$host</li>";
    foreach ($dirout as $line) {
        if (strpos($line, "\t") === 0) {
            $line = substr($line, 1);
            if (strpos($line, "Sharename") === 0) {
                $listingtype = 'shares';
            } elseif (strpos($line, '--------') === 0) {
                # Just ignore this line
            } else {
                switch ($listingtype) {
                    case 'shares' :
                        $pntr = strpos($line, " ");
                        $sharename = substr($line, 0, $pntr);
                        $line = substr($line, $pntr + 1);
                        $line = trim($line);
                        $pntr = strpos($line, " ");
                        if ($pntr > 0) {
                            $sharetype = substr($line, 0, $pntr);
                            $line = substr($line, $pntr + 1);
                            $sharecomment = trim($line);
                        } else {
                            $sharetype = trim($line);
                            $sharecomment = '';
                        }
                        $share['name'] = $sharename;
                        $share['type'] = $sharetype;
                        $share['comment'] = $sharecomment;
                        if (strlen($sharename) - 1 == strrpos($sharename,"$")) {
                            if ($show_sys_shares) {
                                $shares[] = $share;
                            }
                        } else {
                            $shares[] = $share;
                        }
                        break;
                    default :
                        break;
                }
            }
        } else {
            $listingtype = '';
        }
    }
    # Display the results
    #$this->build_quick_link($host,$user,$pass,'','');
    if (count($shares) > 0) {
        foreach ($shares as $share) {
            $sharename = $share['name'];
            $sharetype = $share['type'];
            $sharecomment = $share['comment'];
            $diskimg = '/common/images/iDisk-48x48.png';
            $ipcimg = '/common/images/mac-48x48.png';
            $defualtimg = '/common/images/mac-48x48.png';
            switch ($sharetype) {
                case 'Disk' :
                    $thisimg = $diskimg;
                    break;
                case 'IPC' :
                    $thisimg = $ipcimg;
                    break;
                default :
                    $thisimg = $defualtimg;
                    break;
            }
            $inputid = $this->add_hidden_input("$this->scheme://$host/$sharename");
            $applink = $this->run("browse_smb($inputid:value)->$this->target_id",$this->thisfile);
            $res['title'] = $sharename;
            $res['link'] = $applink;
            $res['img'] = $thisimg;
            $res['details'] = $sharecomment;
            $results[] = $res;
        }
    }
    $this->display_results($results);
}
function smbclient_dir ($host, $user, $pass, $share, $folder) {
    $folder = $this->clean('\\' . $folder, '\\',false);
    if ($folder != '\\') {
        $folder = rtrim($folder,'\\');
    }
    $fullcmd = 'smbclient //' . $host . '/' . $share;
    if ($user != '') {
        $fullcmd .= ' -U ' . $user;
        if ($pass != '') {
            $fullcmd .= '%' . $pass;
        }
    }
    if ($folder != '') {
        $fullcmd .= " -c 'cd \"$folder\"; dir; exit;'";
    } else {
        $fullcmd .= " -c 'dir; exit;'";
    }
    #echo "<li style=\"color:black;\">$host\\$share" . rtrim($folder,'\\') . "</li>";
    $ret = exec($fullcmd, $dirout);
    $listing = true;
    foreach ($dirout as $line) {
        $line = trim($line);
        if ($listing and $line != '') {
            if (substr_count($line, "\t") == 0) {
                $flastmod = '';
                $fl = $line;
                for ($i=1;$i<=5;$i++) {
                    $pntr = strrpos($line, ' ');
                    $flastmod = substr($line, $pntr) . $flastmod;
                    $line = substr($line, 0, $pntr);
                    $line = trim($line);
                }
                $pntr = strrpos($line, ' ');
                $fsize = substr($line, $pntr + 1);
                $line = substr($line, 0, $pntr);
                $line = trim($line);
                if (substr_count($line, ' ') == 0) {
                    $fname = $line;
                    $ftype = '';
                } else {
                    $pntr = strrpos($line, ' ');
                    $ftype = substr($line, $pntr + 1);

                    $line = substr($line, 0, $pntr);
                    $fname = trim($line);
                }
                if ($fname != '.' and $fname != '..') {
                    if ($fname != '') {
                        $file['name'] = $fname;
                        $file['size'] = $fsize;
                        $file['type'] = $ftype;
                        $file['modified'] = $flastmod;
                        $foldercontents[] = $file;
                    }
                }
            }
        } else {
            $listing = false;
        }
    }
    # Display Results
    #$this->build_quick_link($host,$user,$pass,$share,$folder);
    $foldercnt = 0;
    if (count($foldercontents) > 0) {
        $thisimg = '/common/images/Folder-48x48.png';
        foreach ($foldercontents as $file) {
            if (substr_count($file['type'],'D') > 0) {
                $fname = $file['name'];
                #$foldercnt ++;
                $newpath = $folder . '\\' . $fname;
                #echo "<input type=\"hidden\" name=\"folder" . $foldercnt . "n\" id=\"folder" . $foldercnt . "\" value=\"" . $newpath . "\" />";
                #$applink = $this->run("smbclient_dir('".urlencode($host)."','".urlencode($user)."','".urlencode($pass)."','".urlencode($share)."',folder" . $foldercnt . ":value)->$this->target_id",$this->thisfile);
                $newpath = $this->clean($newpath,'/',true);
                $inputid = $this->add_hidden_input("$this->scheme://$host/$share/$newpath");
                $applink = $this->run("browse_smb($inputid:value)->$this->target_id",$this->thisfile);

                $res['title'] = $fname;
                $res['link'] = $applink;
                $res['img'] = $thisimg;
                $results[] = $res;
            }
        }
        $this->display_results($results);
        $results = null;
        $thisimg = '/common/images/mac-48x48.png';
        foreach ($foldercontents as $file) {
            if (substr_count($file['type'], 'D') == 0) {
                $fname = $file['name'];
                $applink = $this->run("smbclient_dir('".urlencode($host)."','".urlencode($user)."','".urlencode($pass)."','".urlencode($share)."','".urlencode($folder . '\\' . $fname)."')->$this->target_id",$this->thisfile);
                $applink = '';
                $res['title'] = $fname;
                $res['link'] = $applink;
                $res['img'] = $thisimg;
                $res['details'] = $this->conv_bytes($file['size']);
                $results[] = $res;
            }
        }
        $this->display_results($results);
        $results = null;
    }
}
function smbclient_cmd ($host, $user, $pass, $share, $command) {
    $fullcmd = 'smbclient //' . $host . '/' . $share;
    if ($user != '') {
        $fullcmd .= ' -U ' . $user;
        if ($pass != '') {
            $fullcmd .= '%' . $pass;
        }
    }
    $fullcmd .= ' -c \'' . $command . '; exit;\'';
    echo $fullcmd . "<br />\n";
    $ret = trim(shell_exec($fullcmd));
    $ret .= "<br />\nDone.<br />\n";
    return $ret;
}
function smbclient_get ($host, $user, $pass, $share, $srcfile, $destfile) {
    $srcfile = $this->clean($srcfile, '\\',false);
    $destfile = $this->clean($destfile, '/',false);
    $fullcmd = 'smbclient //' . $host . '/' . $share;
    if ($user != '') {
        $fullcmd .= ' -U ' . $user;
        if ($pass != '') {
            $fullcmd .= '%' . $pass;
        }
    }
    $fullcmd .= " -c 'get \"$srcfile\" \"$destfile\";'";
    $ret = $fullcmd . " at " . time() . "<br />\n";
    $shell_ret = '';
    $shell_ret = shell_exec($fullcmd);
    if ($shell_ret == '') {
        $ret .= "File transfered.<br />\n";
    } else {
        $ret .= "$shell_ret<br />\nError<br />\n";
    }
    return $ret;
}
function smbclient_put ($host, $user, $pass, $share, $srcfile, $destfile) {
    $srcfile = $this->clean($srcfile, '\\',false);
    $destfile = $this->clean($destfile, '/',false);
    $fullcmd = 'smbclient //' . $host . '/' . $share;
    if ($user != '') {
        $fullcmd .= ' -U ' . $user;
        if ($pass != '') {
            $fullcmd .= '%' . $pass;
        }
    }
    $fullcmd .= " -c 'put \"$srcfile\" \"$destfile\";'";
    $ret = $fullcmd . " at " . time() . "<br />\n";
    $shell_ret = '';
    $shell_ret = shell_exec($fullcmd);
    if ($shell_ret == '') {
        $ret .= "File transfered.<br />\n";
    } else {
        $ret .= "$shell_ret<br />\nError<br />\n";
    }
    return $ret;
}
function clean($path, $use_slash, $remove_preceding_slash) {
    $path = trim($path);
    if ($use_slash == '') {
        $use_slash = '/';
    }
    if (empty($path)) {
        $path = JPATH_ROOT;
    } else {
        // Remove double slashes and backslahses and convert all slashes and backslashes to DS
        $outpath = ereg_replace("\.[\.]+", "", $path);
        if ($remove_preceding_slash) {
            $outpath = ereg_replace("^[\/]+", "", $outpath);
        }
        $outpath = preg_replace('#[/\\\\]+#', $use_slash, $outpath);
        $outpath = ereg_replace("^[A-Za-z][:\|][\/]?", "", $outpath);
    }
    return $outpath;
}
function clear() {
    return '';
}

######################################
} ?>